﻿using System;

namespace ECommerce.Administration.Controls
{
	public partial class EditPaymentMethod : AdminBaseUsercontrol
	{
		private int _ThePaymentMethodID;
		private Service.IPaymentMethodService _PaymentMethodService;

		public int ThePaymentMethodID
		{
			get
			{
				if(ViewState["PaymentMethodID"] != null)
				{
					_ThePaymentMethodID = int.Parse(ViewState["PaymentMethodID"].ToString());
				}
				else
				{
					_ThePaymentMethodID = CommonHelper.QueryStringInt("paymentmethodid");
				}
				return _ThePaymentMethodID;
			}
			set
			{
				_ThePaymentMethodID = value;
				ViewState["PaymentMethodID"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				if (ThePaymentMethodID > 0)
				{
					LabelEditPaymentMethodTitle.Text = GetLocalResourceString("Admin.EditPaymentMethod.Edit.Title");
				}
				else
				{
					LabelEditPaymentMethodTitle.Text = GetLocalResourceString("Admin.EditPaymentMethod.Add.Title");
				}

				ButtonSave.Text = GetLocalResourceString("Admin.SaveButtonText");
				
				SetValues();
			}
		}

		protected void ButtonSave_Click(object sender, EventArgs e)
		{
			Data.Entity.PaymentMethod thePaymentMethod = new Data.Entity.PaymentMethod();
			thePaymentMethod.Name = TextPaymentMethodName.Text.Trim();
			thePaymentMethod.VisibleName = TextVisibleName.Text.Trim();
			thePaymentMethod.UserTemplatePath = string.Empty;
			thePaymentMethod.SystemKeyword = string.Empty;
			thePaymentMethod.IsActive = CheckIsActive.Checked;
			thePaymentMethod.DisplayOrder = int.Parse(TextDisplayOrder.Text.Trim());
			thePaymentMethod.Description = TextDescription.Text.Trim();
			thePaymentMethod.ConfigureTemplatePath = string.Empty;
			thePaymentMethod.ClassName = string.Empty;

			if (_PaymentMethodService == null)
			{
				_PaymentMethodService = new Service.PaymentMethodService();
			}
			
			var thePaymentMethodID = CommonHelper.QueryStringInt("categoryid");
			if(ThePaymentMethodID > 0)
			{
				thePaymentMethod.PaymentMethodID = ThePaymentMethodID;
				_PaymentMethodService.UpdatePaymentMethod(thePaymentMethod);
			}
			else
			{
				ThePaymentMethodID = _PaymentMethodService.CreateNewPaymentMethod(thePaymentMethod);
			}
		}

		private void SetValues()
		{
			if(ThePaymentMethodID > 0)
			{
				if (_PaymentMethodService == null)
				{
					_PaymentMethodService = new Service.PaymentMethodService();
				}

				var thePaymentMethod = _PaymentMethodService.GetThePaymentMethod(ThePaymentMethodID);

				TextPaymentMethodName.Text = thePaymentMethod.Name;
				TextDescription.Text = thePaymentMethod.Description;
				TextDisplayOrder.Text = thePaymentMethod.DisplayOrder.ToString();
				TextVisibleName.Text = thePaymentMethod.VisibleName;
				CheckIsActive.Checked = thePaymentMethod.IsActive;
			}
		}
	}
}